package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.WarehouseComputerPracticeDao;
import com.wing.product.dao.WarehouseMaterialRequisitionRecordDetailDao;
import com.wing.product.model.entity.WarehouseComputerPractice;
import com.wing.product.model.entity.WarehouseMaterialRequisitionRecord;
import com.wing.product.model.entity.WarehouseMaterialRequisitionRecordDetail;
import com.wing.product.model.entity.WarehouseProductionSchedulingPlanDetail;
import com.wing.product.model.request.WarehouseComputerPracticeForm;
import com.wing.product.model.response.WarehouseComputerPracticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class WarehouseComputerPracticeServiceImpl extends ServiceImpl<WarehouseComputerPracticeDao, WarehouseComputerPractice> implements WarehouseComputerPracticeService{

    @Autowired
    WarehouseMaterialRequisitionRecordDetailDao warehouseMaterialRequisitionRecordDetailDao;
    @Autowired
    WarehouseMaterialRequisitionRecordDetailService warehouseMaterialRequisitionRecordDetailService;
    @Autowired
    WarehouseProductionSchedulingPlanDetailService warehouseProductionSchedulingPlanDetailService;
    @Autowired
    WarehouseMaterialRequisitionRecordService warehouseMaterialRequisitionRecordService;

    @Override
    public Page<WarehouseComputerPracticeVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseComputerPracticeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseComputerPracticeVO selectComputerPractice(String id){

        WarehouseComputerPracticeVO warehouseComputerPracticeVO = this.baseMapper.selectComputerPractice(id);
        LambdaQueryWrapper<WarehouseMaterialRequisitionRecordDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseMaterialRequisitionRecordDetail::getComputerPracticeId,warehouseComputerPracticeVO.getId())
                .eq(WarehouseMaterialRequisitionRecordDetail::getIsDeleted,0);
        WarehouseMaterialRequisitionRecordDetail warehouseMaterialRequisitionRecordDetail = warehouseMaterialRequisitionRecordDetailDao.selectOne(lambdaQueryWrapper);
        warehouseComputerPracticeVO.setWarehouseMaterialRequisitionRecordDetail(warehouseMaterialRequisitionRecordDetail);
        return warehouseComputerPracticeVO;
    }

    @Override
    @Transactional
    public JsonResult saveComputerPractice(WarehouseComputerPracticeForm warehouseComputerPracticeForm){

        if (ObjectUtil.isNotEmpty(warehouseComputerPracticeForm)){
            WarehouseComputerPractice warehouseComputerPractice = new WarehouseComputerPractice();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseComputerPractice.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseComputerPracticeForm, warehouseComputerPractice,copyOptions);
            warehouseComputerPractice.setComputerPracticeCode(UniqueIdGeneratorUtil.generateUniqueId("SJ", "yyyyMMddHHmmss", 1000, 9999));
            warehouseComputerPractice.setStatus("1");
            this.baseMapper.insert(warehouseComputerPractice);
            //修改领料及生产计划状态
            if (ObjectUtil.isNotEmpty(warehouseComputerPracticeForm.getWarehouseMaterialRequisitionRecordDetail())){
                warehouseComputerPracticeForm.getWarehouseMaterialRequisitionRecordDetail().setComputerPracticeId(warehouseComputerPractice.getId());
                warehouseComputerPracticeForm.getWarehouseMaterialRequisitionRecordDetail().setStatus("2");
                warehouseMaterialRequisitionRecordDetailDao.updateById(warehouseComputerPracticeForm.getWarehouseMaterialRequisitionRecordDetail());

                LambdaQueryWrapper<WarehouseMaterialRequisitionRecordDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(WarehouseMaterialRequisitionRecordDetail::getRequisitionRecordId,warehouseComputerPracticeForm.getMaterialRequisitionRecordId())
                        .eq(WarehouseMaterialRequisitionRecordDetail::getIsDeleted,0);
                List<WarehouseMaterialRequisitionRecordDetail> warehouseMaterialRequisitionRecordDetails = warehouseMaterialRequisitionRecordDetailDao.selectList(lambdaQueryWrapper);
                Boolean b = true;
                for (WarehouseMaterialRequisitionRecordDetail wr : warehouseMaterialRequisitionRecordDetails){
                    if (wr.getStatus().equals("1")){
                        b = false;
                    }
                }
                if (b){
                    LambdaUpdateWrapper<WarehouseMaterialRequisitionRecord> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    lambdaUpdateWrapper.eq(WarehouseMaterialRequisitionRecord::getId,warehouseComputerPracticeForm.getMaterialRequisitionRecordId())
                            .set(WarehouseMaterialRequisitionRecord::getStatus,"2");
                    warehouseMaterialRequisitionRecordService.update(lambdaUpdateWrapper);
                }

                LambdaUpdateWrapper<WarehouseProductionSchedulingPlanDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(WarehouseProductionSchedulingPlanDetail::getId,warehouseComputerPracticeForm.getProductionSchedulingPlanDetailId())
                        .set(WarehouseProductionSchedulingPlanDetail::getStatus,"5");
                warehouseProductionSchedulingPlanDetailService.update(lambdaUpdateWrapper);
            }
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.fail("操作失败");
        }
    }

    @Override
    public  JsonResult deleteComputerPractice(String id){
        LambdaUpdateWrapper<WarehouseComputerPractice> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WarehouseComputerPractice::getId,id)
                .set(WarehouseComputerPractice::getIsDeleted,1);
        this.update(lambdaUpdateWrapper);
        LambdaUpdateWrapper<WarehouseMaterialRequisitionRecordDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WarehouseMaterialRequisitionRecordDetail::getComputerPracticeId,id)
                .set(WarehouseMaterialRequisitionRecordDetail::getComputerPracticeId,null)
                .set(WarehouseMaterialRequisitionRecordDetail::getStatus,1);
        warehouseMaterialRequisitionRecordDetailService.update(updateWrapper);
        return JsonResult.success("删除成功");
    }
}
